
package com.aol.expressions.calendar.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This Servlet is used to mock update calendar item handler.
 * 
 * @author 
 * @version 1.0
 */
public class MockUpdateCalendarItemHandler extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, FileNotFoundException, URISyntaxException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String name = request.getParameter("name");
            String [] expr = request.getParameterValues("expr") ;
            String f =request.getParameter("f");
            if (f != null && f.equals("json")) 
           {
               if (name != null && name.equals("cat"))
               {
               out.print(getTextFromFile("/test/calendar/updateCalendarItemByName.json"));
               }
               
           }
            if (f != null && f.equals("json")) 
           {
               if(expr.length == 1)
               {
                 out.print(getTextFromFile("/test/calendar/updateCalendarItemOne.json"));  
               }
               else 
               {
                out.print(getTextFromFile("/test/calendar/updateCalendarItemThree.json"));     
               }
               
           }
        } finally { 
            out.close();
        }
    } 
    
    /**
     * This method is used to get json text from test json file.
     * 
     * @param fileName json file name
     * @return json text
     * @throws java.io.FileNotFoundException if file not found
     * @throws java.net.URISyntaxException if uri syntaax errors
     * @throws java.io.IOException if io error
     */
private String getTextFromFile(String fileName) throws FileNotFoundException, URISyntaxException, IOException
    {
      BufferedReader in = new BufferedReader(new FileReader(new File(this.getClass().getClassLoader().getResource(fileName).toURI())));
      String str;
      StringBuffer sb = new StringBuffer();
      while ((str = in.readLine()) != null){
          
          sb.append(str);
      }
      in.close();
      return sb.toString();
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet 方法。单击左侧的 + 号以编辑代码。">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, FileNotFoundException {
        try {
            processRequest(request, response);
        } catch (URISyntaxException ex) {
            Logger.getLogger(MockUpdateCalendarItemHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, FileNotFoundException {
        try {
            processRequest(request, response);
        } catch (URISyntaxException ex) {
            Logger.getLogger(MockUpdateCalendarItemHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
